package
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import modules.march.Block;
	import modules.march.MapBoard;
	import modules.menu.Menu;
	import modules.pack.PackData;
	import morn.core.handlers.Handler;
	import morn.core.managers.ResLoader;
	import util.Textures;
	import util.Tools;
	
	[SWF(width="800",height="500",backgroundColor="0XCEE7FF")]
	
	/**
	 * 主界面
	 * @author WLDragon 2014-02-19
	 */
	public class Main extends Sprite
	{
		
		public function Main():void
		{
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//MonsterDebugger.initialize(this);
			var resArr:Array = [
			{url:"assets/comp.swf", type:ResLoader.SWF, size:100 },
			{url:"assets/texture.swf", type:ResLoader.SWF, size:100 },
			{url:"assets/config.xml", type:ResLoader.TXT, size:100 }
			];
			
			App.init(this);
			App.loader.loadAssets(resArr, new Handler(loadComplete));
		
			//var s:Stats = new Stats;
			//s.y = App.stage.stageHeight - 80;
			//addChild(s);
		}
		
		private function loadComplete():void
		{
			functionPanel = new Sprite();
			addChild(functionPanel);
			
			menu = new Menu();
			addChild(menu);
			menu.addEventListener(MouseEvent.CLICK, onClick);
			
			stage.addEventListener(Event.RESIZE,onResize);
			
			//首先打开数据打包
			packData = new PackData();
			showFunction(packData);
		}
		
		private function onResize(e:Event = null):void 
		{
			menu.reSize();
			
			if(marchMapEditor)
				marchMapEditor.reSize();
			
		}
		
		private function onClick(e:MouseEvent):void
		{
			switch (e.target.name)
			{
				case "lnkPackData": 
					if (packData == null)
						packData = new PackData();
					
					showFunction(packData);
					break;
				case "lnkEditMarchMap": 
					if (marchMapEditor == null)
					{
						Tools.textures.initTexture("png.texture.map", Textures.COL, Textures.ROW);
						Tools.pools.createPool(Block, 640);
						marchMapEditor = new MapBoard();
						onResize();
					}
					showFunction(marchMapEditor);
					break;
				
				default: 
			}
		}
		
		private function showFunction(display:DisplayObject):void
		{
			if (functionPanel.numChildren > 0)
				functionPanel.removeChildAt(0);
			
			functionPanel.addChild(display);
		}
		
		/**菜单*/
		private var menu:Menu;
		/**功能面板，存放当前功能的容器*/
		private var functionPanel:Sprite;
		/**行军地图编辑器*/
		private var marchMapEditor:MapBoard;
		/**数据打包*/
		private var packData:PackData;
	}

}